﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_BRND_CT_PRD_VAR_SZE : RibbonForm
    {
        public M_BRND_CT_PRD_VAR_SZE()
        {
            InitializeComponent();
        }


        public string ConnectionString { get; set; }

        public DataTable LineItems { get; set; }
        void RemoveBlankLastRowFirst()
        {
            int iCount = -1;
            gridView1.MoveLast();

            int[] oRow = gridView1.GetSelectedRows();

            iCount = oRow[0];
            string sItemCode = "";


            sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["Size"]).ToString();

            if (sItemCode.Trim() == "")
            {
                gridView1.DeleteRow(iCount);
            }


        }
        public DataTable GetLineItems()
        {
            DataTable table = new DataTable();

            table.Columns.Add("BrandID", typeof(string));
            table.Columns.Add("CatgryCode", typeof(string));
            table.Columns.Add("ProdCode", typeof(string));
            table.Columns.Add("VrntCode", typeof(string));
            table.Columns.Add("Size", typeof(string));
            table.Columns.Add("UntMsr", typeof(string));

            return table;
        }

        void BindSizes(string _BrandID, string _CategoryCode, string _ProductCode, string _VariantCode)
        {
            ProductSizeManager oSizeManager = new ProductSizeManager();

            oSizeManager.ConnectionString = this.ConnectionString;
            oSizeManager.Open();

            DataTable table = oSizeManager.GetSizes(_BrandID, _CategoryCode, _ProductCode, _VariantCode);

            oSizeManager.Close();

            this.LineItems = table;
            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();

            if (gridView1.DataRowCount <= 0)
            {
                gridView1.AddNewRow();
            }

        }

        void BindBrand(string ItemGroup)
        {
            BrandManager oManager = new BrandManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table = oManager.GetBrands(ItemGroup);
            oManager.Close();

            cboBrand.DisplayMember = "BrandName";
            cboBrand.ValueMember = "BrandID";
            cboBrand.DataSource = _table;
            cboBrand.Refresh();

            if (cboBrand.Items.Count > 0)
            {
                cboBrand.SelectedIndex = 0;
            }
            else
            {
                BindCategory("");
                BindProduct("", "");
                BindVariant("", "", "");
            }
        }

        void BindCategory(string _BrandID)
        {
            BrandCategoryManager oManager = new BrandCategoryManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table = oManager.GetCategoryByBrandID(_BrandID);
            oManager.Close();

            cboCategory.DisplayMember = "CatgryName";
            cboCategory.ValueMember = "CatgryCode";
            cboCategory.DataSource = _table;
            cboCategory.Refresh();

            if (cboCategory.Items.Count > 0)
            {
                cboCategory.SelectedIndex = 0;
                //BindProduct(cboBrand.SelectedValue.ToString(), cboCategory.SelectedValue.ToString());
            }
            else
            {
                BindProduct("", "");
                BindVariant("", "", "");
            }

        }

        void BindProduct(string Brand, string Category)
        {
            ProductManager oManager = new ProductManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table = oManager.GetProducts(Brand, Category);
            oManager.Close();

            cboProduct.DisplayMember = "ProductName";
            cboProduct.ValueMember = "ProdCode";
            cboProduct.DataSource = _table;
            cboProduct.Refresh();

            if (cboProduct.Items.Count > 0)
            {
                cboProduct.SelectedIndex = 0;
            }
            else
            {
                BindVariant("", "", "");
            }
        }

        void BindVariant(string _BrandID, string _CatCode, string _ProductCode)
        {
            ProductVariantManager oManager = new ProductVariantManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable table = oManager.GetVariants(_BrandID, _CatCode, _ProductCode);
            oManager.Close();

            cboVariant.DisplayMember = "VrntName";
            cboVariant.ValueMember = "VrntCode";
            cboVariant.DataSource = table;
            cboVariant.Refresh();
        }
        private void cboItemGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindBrand(cboItemGroup.Text);
        }

        private void cboBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCategory(cboBrand.SelectedValue.ToString());
        }

        private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProduct(cboBrand.SelectedValue.ToString(), cboCategory.SelectedValue.ToString());
        }

        private void cboProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindVariant(cboBrand.SelectedValue.ToString(), cboCategory.SelectedValue.ToString(), cboProduct.SelectedValue.ToString());
        }

        private void cboVariant_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindSizes(cboBrand.SelectedValue.ToString(), cboCategory.SelectedValue.ToString(), cboProduct.SelectedValue.ToString(), cboVariant.SelectedValue.ToString());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ProductSizeManager oManager = new ProductSizeManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            RemoveBlankLastRowFirst();
            oManager.UpdateSize(this.LineItems);
            oManager.Close();
            MessageBox.Show("Updated");
        }

        private void M_BRND_CT_PRD_VAR_SZE_Load(object sender, EventArgs e)
        {
            this.LineItems = GetLineItems();
            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();
           

            if (gridView1.DataRowCount <= 0)
            {
                gridView1.AddNewRow();
            }

        }

        private void txtSize_EditValueChanged(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;
            
            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["BrandID"], cboBrand.SelectedValue.ToString());
            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["CatgryCode"], cboCategory.SelectedValue.ToString());
            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["ProdCode"], cboProduct.SelectedValue.ToString());
            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["VrntCode"], cboVariant.SelectedValue.ToString());
        }

        private void txtUoM_EditValueChanged(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;

            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["BrandID"], cboBrand.SelectedValue.ToString());
            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["CatgryCode"], cboCategory.SelectedValue.ToString());
            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["ProdCode"], cboProduct.SelectedValue.ToString());
            gridView1.SetRowCellValue(iRowIndex, gridView1.Columns["VrntCode"], cboVariant.SelectedValue.ToString());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                int iCount = -1;
                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];

                if (e.KeyCode == Keys.Delete)
                {
                    if (MessageBox.Show("Are you sure you want to delete this Row?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        
                        string sProductSize = "";

                        oRow = gridView1.GetSelectedRows();
                        iCount = oRow[0];
                        sProductSize = gridView1.GetRowCellValue(iCount, gridView1.Columns["Size"]).ToString();

                        if (sProductSize != "")
                        {
                            gridView1.DeleteRow(iCount);
                        }

                        gridView1.RefreshData();
                        

                    }
                }


                if (e.KeyCode == Keys.Down)
                {
                    if (gridView1.IsLastRow)
                    {
                        string sSize = "";
                        oRow = gridView1.GetSelectedRows();

                        iCount = oRow[0];
                        sSize = gridView1.GetRowCellValue(iCount, gridView1.Columns["Size"]).ToString();

                        gridView1.AddNewRow();
                        
                    }
                }
            }
            catch
            {
            }
        }
    }
}
